﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Model;
using ZLCMS.Business;
using NHibernate;
using DotNet.Common.Command;
using ZLCMS.Commands.Sys.Commands.RightInfo;

namespace ZLCMS.Web.SystemManage.Basics.RightConfig.Right
{
    public partial class RightList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected UrConfig Group { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            using(ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                group.DataSource = session.CreateQuery("from UrConfig where ItemGroup='RightGroup'").List<UrConfig>();
                group.DataTextField = "ItemValue";
                group.DataValueField = "ItemName";
                group.DataBind();

                string groupValue = Request["group"];
                if (!string.IsNullOrEmpty(groupValue))
                {
                    group.SelectedValue = groupValue;
                }

                Group = session.CreateQuery("from UrConfig where ItemGroup='RightGroup' and ItemName=:code")
                    .SetParameter("code", group.SelectedValue)
                    .UniqueResult<UrConfig>();
            }
            _framePage = new FrameHelper(this);

            LoadRightListParameter param = new LoadRightListParameter();

            param.PageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;
            param.PageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;
            param.GroupValueFilter = group.SelectedValue;
            ICommandResult<LoadRightListResult> result = new LoadRightListCommand().Execute(param);
            if (!result.Error)
            {
                _framePage.RecordCount = result.TotalCount;
                grid.DataSource = result.Result.List;
                grid.DataBind();
            }
            else
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = result.ErrorMsg });
            }
        }
    }
}
